package com.android.love.adapter;

import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.love.R;
import com.android.love.util.Constant;
import com.android.love.util.LogUtil;
import com.android.love.vo.Address;
import com.android.love.vo.Goods;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class MyAddressAdapter extends BaseAdapter implements Constant {
	private static final String TAG="MyAddressAdapter";
	private List<Address> address_list = Collections.emptyList();;
	private Context context;
	private Handler handler;

	public MyAddressAdapter(Context context, List<Address> address_list, Handler handler) {
		super();
		this.context = context;
		this.address_list = address_list;
		this.handler=handler;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return address_list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return address_list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// ViewHolder holder;
		// if (convertView == null) {
		convertView = View.inflate(context, R.layout.address_item, null);
		// holder = new ViewHolder();
		final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
		ImageView iv_edit = (ImageView) convertView.findViewById(R.id.iv_edit);
		TextView tv_myaddrss = (TextView) convertView
				.findViewById(R.id.tv_myaddrss);
		Address address = address_list.get(position);
		tv_myaddrss.setText(address.getConsignee() + "  " + address.getPhone()
				+ "\n" + address.getAddress() + "   " + address.getPostal()
				+ "\n" + address.getMail());
		checkBox.setChecked(address.getDefault_address() == 1);
		iv_edit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sendMessage(ADDRESS_EDIT_MSG, position);
				LogUtil.i(TAG, "iv_edit ---" + address_list.size());
			}
		});
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				sendMessage(ADDRESS_EDIT_MSG, position);
				LogUtil.i(TAG, "iv_edit ---" + address_list.size());
			}
		});
//		if(checkBox.isChecked()){
//			checkBox.setfo
//		}
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					sendMessage(ADDRESS_CHECK, position);
				}else{
					sendMessage(ADDRESS_NOT_CHECK, position);
					checkBox.setChecked(true);
//					notifyDataSetChanged();
				}

			}
		});
		return convertView;
	}

	/**
	 * 发消息
	 * 
	 * @param what
	 * @param position
	 */
	private void sendMessage(int what, int position) {
		Message msg = Message.obtain();
		msg.what = what;
		msg.arg1 = position;
		handler.sendMessage(msg);
	}

	// public static class ViewHolder {
	// CheckBox checkBox;
	// ImageView iv_cart_img;
	// ImageView iv_add;
	// ImageView iv_reduce;
	// TextView tv_cart;
	// TextView tv_format;
	// TextView tv_price;
	// TextView tv_num;
	// TextView tv_id;
	// }

}
